//
//  Utils.m
//  Medical_Wisdom
//
//  Created by Mac on 14-1-26.
//  Copyright (c) 2014年 NanJingXianLang. All rights reserved.
//

#import "Utils.h"

#import "AppDelegate.h"

@implementation Utils

/*
 AppDelegate
 */
+ (AppDelegate *)applicationDelegate
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

+ (UIImageView *)imageViewWithFrame:(CGRect)frame withImage:(UIImage *)image{
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = image;
    return imageView;
}

+ (UIImageView *)imageViewWithImage:(UIImage *)image{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = image;
    return imageView;
}

+ (UILabel *)labelWithFrame:(CGRect)frame withTitle:(NSString *)title titleFontSize:(CGFloat)font textColor:(UIColor *)color backgroundColor:(UIColor *)bgColor alignment:(NSTextAlignment)textAlignment{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.font = FontVlaue(font);
    label.textColor = color;
    label.backgroundColor = bgColor;
    label.textAlignment = textAlignment;
    return label;
}

+ (UILabel *)labelWithTitle:(NSString *)title titleFontSize:(CGFloat )font textColor:(UIColor *)color backgroundColor:(UIColor *)bgColor alignment:(NSTextAlignment)textAlignment{
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.font = FontVlaue(font);
    label.textColor = color;
    label.backgroundColor = bgColor;
    label.textAlignment = textAlignment;
    return label;
}

+ (UIButton *)createBtnWithType:(UIButtonType)btnType frame:(CGRect)btnFrame backgroundColor:(UIColor*)bgColor title:(NSString *)title image:(NSString *)image font:(CGFloat)font{
    UIButton *btn = [UIButton buttonWithType:btnType];
    btn.frame = btnFrame;
    btn.titleLabel.font = FontVlaue(font);

    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:image] forState:(UIControlStateNormal)];
    [btn setBackgroundColor:bgColor];
    return btn;
}

+(UIButton *)createBtnWithType:(UIButtonType)btnType backgroundColor:(UIColor*)bgColor title:(NSString *)title image:(NSString *)image font:(CGFloat)font{
    UIButton *btn = [UIButton buttonWithType:btnType];
    [btn setTitle:title forState:(UIControlStateNormal)];
    btn.titleLabel.font = FontVlaue(font);
    [btn setImage:[UIImage imageNamed:image] forState:(UIControlStateNormal)];
    [btn setBackgroundColor:bgColor];
    return btn;

}

//利用正则表达式验证邮箱的合法性
+(BOOL)isValidateEmail:(NSString *)email {
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
    
}

//身份证号判断
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

//银行卡号判断
+ (BOOL) checkCardNo:(NSString*) cardNo
{
    int oddsum = 0;     //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
    
    cardNo = [cardNo substringToIndex:cardNoLength - 1];
    for (int i = cardNoLength -1 ; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1 ) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}

//手机号码验证
+ (BOOL)validatePhone:(NSString *)phone{
    NSString *phoneRegex = @"1[3|5|7|8|][0-9]{9}";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:phone];
}

//空字符串的判断
+ (BOOL)isNull:(id)object{
    // 判断是否为空串
    if ([object isEqual:[NSNull null]]) {
        return NO;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return NO;
    }
    else if (object==nil){
        return NO;
    }
    return YES;
}

//md5加密
+ (NSString *)md5:(NSString *)str{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+(UIAlertController *)alertCreateTitle:(NSString *)title
                            andMassage:(NSString *)massage {
    
    return [self alertActionCreateTitle:title andMassage:massage setActionEnterText:nil setActionCancelText:@"确定" setEnterAction:nil];
}

+(UIAlertController *)alertOneAcitonCreateTitle:(NSString *)title
                                     andMassage:(NSString *)massage
                             setActionEnterText:(NSString *)enterTitle
                                 setEnterAction:(void(^)(UIAlertAction * action))handler {
    
    return [self alertActionCreateTitle:title andMassage:massage setActionEnterText:enterTitle setActionCancelText:nil setEnterAction:handler];
}


+(UIAlertController *)alertActionCreateTitle:(NSString *)title
                                  andMassage:(NSString *)massage
                          setActionEnterText:(NSString *)enterTitle
                         setActionCancelText:(NSString *)cancelTitle
                              setEnterAction:(void(^)(UIAlertAction * action))handler {
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title
                                                                    message:massage preferredStyle:UIAlertControllerStyleAlert];
    
    //    UIAlertAction * actionEnter = [[UIAlertAction alloc]init];
    if (enterTitle) {
        UIAlertAction * actionEnter = [UIAlertAction actionWithTitle:enterTitle
                                                               style:UIAlertActionStyleDestructive
                                                             handler:handler];
        [alert addAction:actionEnter];
    }
    if (cancelTitle) {
        UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:cancelTitle
                                                                style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:actionCancel];
    }
    
    return alert;
}


@end
